﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizShpCustomerCart : CustBizObject
    {
        private CDmShpCustomerCart m_CDmShpCustomerCart = new CDmShpCustomerCart();
        private CDmShpCustomerCart oDmShpCustomerCart
        {
            get { return m_CDmShpCustomerCart; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpCustomerCart()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpCustomerCart.SHP_CUSTOMER_CART);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["CART_ORDER_ID"]; 
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            //drw["SELLER_DEL"] = false;
            //drw["BUYER_DEL"] = false;
            //drw["BUYER_RATE_SID"] = "";

            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            } 
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        { 
            return true;
        }

        public DataSet SelectByIds(string strIdS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT A.*,SHOP_GOODS_QTY=B.GOODS_QTY FROM " + this.TableName + " A LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.PUB_NORMS_ID=B.PUB_NORMS_ID WHERE  A." + this.KeyFieldName + " IN  ('" + strIdS.Replace("'", "").Replace(",", "','") + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectByPubNid(string strPubNid,string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  (PUB_NORMS_ID ='" + strPubNid + "') AND (USER_NME='" + strUserNme + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectCartStatistic(string strusernme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT    
    PRICE_DOWN =SUM(CASE WHEN B.GOODS_STRIKE_PRICE<A.GOODS_STRIKE_PRICE THEN 1 ELSE 0 END)
    ,KUCUN_DOWN =SUM(CASE WHEN B.GOODS_QTY <5 THEN 1 ELSE 0 END) 
    ,ALL_GOODS=COUNT(*)
    FROM [dbo].[SHP_CUSTOMER_CART] A
    LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.PUB_NORMS_ID=B.PUB_NORMS_ID
    WHERE
    A.USER_NME='" + strusernme+"'");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public string GetUserCartJson(string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
 SELECT A.[CART_ORDER_ID] 
      ,A.[COMM_TENANT_ID]
      ,A.[COMM_TENANT_NME]
      ,A.[COMM_USER_NME]
      ,A.[GOODS_ID]
      ,A.[GOODS_NME]
      ,A.[IMG_URL]
      ,A.[PUB_NORMS_ID]
      ,A.[NORMS_DESC]
      ,A.[GOODS_QTY]
      ,GOODS_OLD_PRICE=A.[GOODS_STRIKE_PRICE] 
      ,B.[GOODS_STRIKE_PRICE] 
      ,SHOP_GOODS_QTY=B.[GOODS_QTY] 
      ,SHOP_AMT=A.[GOODS_QTY] *B.[GOODS_STRIKE_PRICE]
      ,IS_DEL=CASE WHEN B.PUB_NORMS_ID IS NULL THEN '1' ELSE '0' END
  FROM [dbo].[SHP_CUSTOMER_CART] A
  LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.PUB_NORMS_ID=B.PUB_NORMS_ID  
" + " WHERE A.USER_NME='" + strUserNme + "'");

            string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "COMM_TENANT_ID,COMM_TENANT_NME,COMM_USER_NME", 1, 1000, "");
            return strjsontem1;
        }

        /// <summary>
        /// 返回单前货物件数
        /// </summary>
        /// <param name="strGoodsId"></param>
        /// <param name="strCOrderId"></param>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="sqty"></param>
        /// <returns></returns>
        public int AddUserCart(string strGoodsId, string strCOrderId, string p1, string p2, string sqty)
        {
            try
            {

                if (!string.IsNullOrEmpty(strCOrderId))
                {
                    DataSet ds = this.SelectByIds(strCOrderId);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        int ifirst = 0;
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            if (string.IsNullOrEmpty(sqty))
                            {
                                ds.Tables[0].Rows[0].Delete();
                            }
                            else
                            {
                                int inew = int.Parse(sqty) + (int)ds.Tables[0].Rows[0]["GOODS_QTY"];
                                if (i == 0)
                                {
                                    ifirst = inew;
                                }
                                if (inew <= 0 || string.IsNullOrEmpty(sqty))
                                {
                                    ds.Tables[0].Rows[0].Delete();
                                }
                                else
                                {
                                    if ((int)ds.Tables[0].Rows[0]["SHOP_GOODS_QTY"] < inew)
                                    {
                                        ds.Tables[0].Rows[0]["GOODS_QTY"] = ds.Tables[0].Rows[0]["SHOP_GOODS_QTY"];
                                        ifirst = (int)ds.Tables[0].Rows[0]["SHOP_GOODS_QTY"];
                                    }
                                    else
                                    {
                                        ds.Tables[0].Rows[0]["GOODS_QTY"] = inew;
                                    }
                                }
                            }
                        }



                        this.Update(ds);
                        return ifirst;
                    }
                }

                if (string.IsNullOrEmpty(sqty))
                {
                    return 0;
                }

                if (int.Parse(sqty) <= 0)
                {
                    return 0;
                }

                if (string.IsNullOrEmpty(strGoodsId))
                {
                    return 0;
                }
                if (string.IsNullOrEmpty(p1))
                {
                    p1 = "";
                }
                if (string.IsNullOrEmpty(p2))
                {
                    p2 = "";
                }

//                using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo objgoods = new AppLogic.DataAlc.CBizShpGoodsInfo())
//                {
//                    DataSet dsList = this.DataObj.QueryBySQL(@"
//	SELECT DISTINCT
//	A.GOODS_ID
//	,C.COMM_TENANT_NME 
//	,A.GOODS_NME
//	,COMM_USER_NME=C.USER_NME
//	,A.IMG_URL
//	,NORMS_DESC=(CASE WHEN B.PUB_NORMS_NME1<>'' THEN  ' '+(CASE WHEN ISNULL(D.NORMS_NME1,'')<>'' THEN D.NORMS_NME1 ELSE '分类' END) + ':'+ B.PUB_NORMS_NME1 ELSE '' END)
//				+ (CASE WHEN B.PUB_NORMS_NME2<>'' THEN  ','+(CASE WHEN ISNULL(D.NORMS_NME2,'')<>'' THEN D.NORMS_NME2 ELSE '型号' END) + ':'+ B.PUB_NORMS_NME2 ELSE '' END)+'  '
//	,B.GOODS_STRIKE_PRICE 
//    ,SHOP_GOODS_QTY=B.[GOODS_QTY] 
//	,B.PUB_NORMS_ID
//	,A.COMM_TENANT_ID 
//	FROM SHP_GOODS_INFO A
//	LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.GOODS_ID=B.GOODS_ID AND (A.NORMS_COUNT=1 OR A.NORMS_COUNT>1 AND (B.PUB_NORMS_NME1='" + p1.Replace("'", "") + @"' AND B.PUB_NORMS_NME2='" + p2.Replace("'", "") + @"'))
//	LEFT OUTER JOIN SHP_COMMERCIAL_TENANT_INFO C ON A.COMM_TENANT_ID=C.COMM_TENANT_ID
//	LEFT OUTER JOIN SHP_GOODS_TYPE_LEVEL_THREE D ON A.LEVEL_THREE_ID=D.LEVEL_THREE_ID 
//	WHERE B.PUB_NORMS_ID IS NOT NULL
//	AND A.GOODS_ID ='" + strGoodsId.Replace("'", "") + @"'");
//                    if (dsList.Tables[0].Rows.Count <= 0)
//                    {
//                        return 0;
//                    }
//                    else
//                    {
//                        DataSet dsOrg = this.SelectByPubNid(dsList.Tables[0].Rows[0]["PUB_NORMS_ID"].ToString(), msBsc.SvRef.UserInfo.Operator);
//                        //DataRow[] arrrowtm = dsOrg.Tables[0].Select("PUB_NORMS_ID='" + dsList.Tables[0].Rows[0]["PUB_NORMS_ID"].ToString() + "'");
//                        DataRow drnew;
//                        if (dsOrg.Tables[0].Rows.Count > 0)
//                        {
//                            drnew = dsOrg.Tables[0].Rows[0];
//                            drnew["GOODS_QTY"] = int.Parse(sqty) + (int)dsOrg.Tables[0].Rows[0]["GOODS_QTY"];
//                        }
//                        else
//                        {
//                            drnew = dsOrg.Tables[0].NewRow();
//                            drnew["CART_ORDER_ID"] = this.DataObj.NewID();
//                            drnew["USER_NME"] = msBsc.SvRef.UserInfo.Operator;
//                            drnew["COMM_TENANT_ID"] = dsList.Tables[0].Rows[0]["COMM_TENANT_ID"];
//                            drnew["PUB_NORMS_ID"] = dsList.Tables[0].Rows[0]["PUB_NORMS_ID"];
//                            drnew["GOODS_QTY"] = int.Parse(sqty);
//                            dsOrg.Tables[0].Rows.Add(drnew);

//                        }
//                        drnew["COMM_TENANT_NME"] = dsList.Tables[0].Rows[0]["COMM_TENANT_NME"];
//                        drnew["COMM_USER_NME"] = dsList.Tables[0].Rows[0]["COMM_USER_NME"];
//                        drnew["GOODS_ID"] = dsList.Tables[0].Rows[0]["GOODS_ID"];
//                        drnew["GOODS_NME"] = dsList.Tables[0].Rows[0]["GOODS_NME"];
//                        drnew["IMG_URL"] = dsList.Tables[0].Rows[0]["IMG_URL"];

//                        drnew["NORMS_DESC"] = dsList.Tables[0].Rows[0]["NORMS_DESC"];
//                        drnew["GOODS_STRIKE_PRICE"] = dsList.Tables[0].Rows[0]["GOODS_STRIKE_PRICE"];

//                        if ((int)drnew["GOODS_QTY"] > (int)dsList.Tables[0].Rows[0]["SHOP_GOODS_QTY"])
//                        {
//                            drnew["GOODS_QTY"] = dsList.Tables[0].Rows[0]["SHOP_GOODS_QTY"];
//                        }
//                        this.Update(dsOrg);
//                        return (int)dsOrg.Tables[0].Rows[0]["GOODS_QTY"];
//                    }
//                }
            }
            catch { }
            return 0;
        }

        //public void UpdateStatisticsInfo(string strOrderId)
        //{
        //    CDbParmCollection parmCollection = new CDbParmCollection();
        //    parmCollection.Add(new CDbParm("strOrderId", ParameterDirection.Input, EDbType.String, 36, strOrderId));
        //    this.DataObj.ExecuteBySP("SHP_UPDATE_STATISTICS_INFO", parmCollection);
        //}

    }
}
